import 'package:flutter/material.dart';
import 'package:flutter_bilibili/http/dao/login_dao.dart';
import 'package:flutter_bilibili/util/business_util.dart';
import 'package:flutter_bilibili/widget/login_effect.dart';
import 'package:flutter_bilibili/widget/login_input.dart';
import 'package:flutter_bilibili/widget/primary_button.dart';
import 'package:flutter_bilibili/widget/top_app_bar.dart';
import 'package:get/get.dart';

/// 注册页面
class RegisterPage extends StatefulWidget {
  const RegisterPage({super.key});

  @override
  State<RegisterPage> createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  late bool protect = false;
  bool registerEnable = false;
  late String username;
  late String password;
  late String rePassword;
  late String imoocId;
  late String orderId;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: topAppBar('注册', '登录', toLoginPage),
      body: ListView(
        // 使用ListView自适应键盘弹起，防止遮挡
        children: [
          LoginEffect(protect: protect),
          LoginInput(
            '用户名',
            onChanged: (text) {
              username = text;
              checkInput();
            },
          ),
          LoginInput(
            '密码',
            obscureText: true,
            focusChanged: (focus) {
              setState(() {
                protect = focus;
              });
            },
            onChanged: (text) {
              password = text;
              checkInput();
            },
          ),
          LoginInput(
            '确认密码',
            onChanged: (text) {
              rePassword = text;
              checkInput();
            },
          ),
          LoginInput(
            '慕课网ID',
            keyboardType: TextInputType.number,
            onChanged: (text) {
              imoocId = text;
              checkInput();
            },
          ),
          LoginInput(
            '课程订单号',
            hint: '请输入课程订单号后四位！',
            keyboardType: TextInputType.number,
            onChanged: (text) {
              orderId = text;
              checkInput();
            },
          ),
          Padding(
            padding: const EdgeInsets.all(20),
            child: PrimaryButton(
              '注册',
              enable: registerEnable,
              onPressed: checkParams,
            ),
          )
        ],
      ),
    );
  }

  /// 校验表单是否填写
  void checkInput() {
    if (username.isNotEmpty &&
        password.isNotEmpty &&
        rePassword.isNotEmpty &&
        imoocId.isNotEmpty &&
        orderId.isNotEmpty) {
      setState(() {
        registerEnable = true;
      });
    } else {
      setState(() {
        registerEnable = false;
      });
    }
  }

  /// 校验请求参数
  void checkParams() {
    String? tips;
    if (password != rePassword) {
      tips = '两次密码不一致！';
    } else if (orderId.length != 4) {
      tips = '请输入订单号后四位！';
    }
    if (tips != null) {
      Get.snackbar('失败', tips, backgroundColor: Colors.red[100]);
    }
    send();
  }

  /// 发送请求
  void send() async {
    try {
      var res = await LoginDao.register(username, password, imoocId, orderId);
      if (res != null && res['code'] == 0) {
        toLoginPage();
      }
    } catch (e) {
      print(e);
    }
  }
}
